﻿Public Partial Class Menu_Popup
    Inherits System.Web.UI.Page
    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Dim lbUser As Label = CType(Master.FindControl("lbMenu"), Label)
            'lbUser.Text = "MENU"
            If Not (Session("MenuPopup") Is Nothing) Then
                ViewState("Menu_no") = Session("MenuPopup").ToString().Split("$")(0)
                txtname.Text = Session("MenuPopup").ToString().Split("$")(1)
                If Not (ddlMode.Items.FindByValue(Session("MenuPopup").ToString().Split("$")(2)) Is Nothing) Then
                    ddlMode.SelectedValue = Session("MenuPopup").ToString().Split("$")(2)
                End If
                txtPath.Text = Session("MenuPopup").ToString().Split("$")(3)
            Else
                ViewState("Menu_no") = ""
            End If
            If (Session("EditMaster") Is Nothing) Then
                btnSave.Visible = False
            End If
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Session("MenuPopup") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal();", True)
    End Sub

    Private Function chkInput() As Boolean
        If (txtname.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุชื่อเมนู !!');", True)
            Return False
        End If
        If (ddlMode.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการเลือกหมวดหมู่ !!');", True)
            Return False
        End If
        If (txtPath.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุ Path !!');", True)
            Return False
        End If
        Return True
    End Function

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (chkInput()) Then
            Dim conn As New System.Data.SqlClient.SqlConnection(connString)
            Dim clsDB As New ClassDatabase
            Dim clsFc As New ClassFunction
            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Application.Lock()
            conn.Open()
            Dim Menu_no As String = ""
            If ViewState("Menu_no").ToString() = "" Then
                Menu_no = clsDB.GeneratorID(conn, "Menu")
            Else
                Menu_no = ViewState("Menu_no").ToString()
            End If

            '  tran = clsDB.BeginTran(conn)
            If (clsFc.InsertMENU(ViewState("Menu_no").ToString(), Menu_no, txtname.Text.Trim(), ddlMode.SelectedItem.Text, txtPath.Text.Trim(), conn, tran) > 0) Then
                Session("MenuPopup") = Nothing
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "parent.closeModal();alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
            Else
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึกข้อมูล !!');", True)
            End If
            conn.Close()
            Application.UnLock()
            conn.Dispose()
        End If
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class